#include <stdio.h>
#include <string.h>

int main()
{
    /*指针就是指向地址*/
#if 0
    /*指针的定义*/
    int num = 100;
    printf("&num = %p,\t *(&num) = %d\n",&num,*(&num));
    /*整型指针*/
    /*一级指针*/
    int * pNum = &num;
    printf("pNum = %p,\t *pNum = %d\n",pNum,*pNum);

    /*二级指针*/
    int **ppNum = &pNum;
    printf("*ppNum=%p,\t **ppNum = %d\n",*ppNum,**ppNum);

    /*字符指针*/
    char * pCh = NULL;
#endif

#if 0
    /*指针的内存*/
    int num = 100;
    int * pNum = &num;

    char sex = 'm';
    char * psex = &sex;

    int len1 = sizeof(num);
    int len2 = sizeof(pNum); 
    int len3 = sizeof(sex); 
    int len4 = sizeof(psex);

    printf("sizeof(num) = %d\nsizeof(pnum) = %d\nsizeof(sex) = %d\nsizeof(num) = %d\n",len1,len2,len3,len4);

    /*在64位系统中，指针的内存占用8个字节*/
#endif

#if 0
    /*字符数组和字符指针*/
    char buffer[]="hello world!";
    /*数组名就是首元素地址*/
     
    /* 字符指针 指向 字符数组*/
    char * pNum = buffer;

    printf("buffer = %p\tbuffer = %s\n",buffer,buffer);
    printf("pNum =%p\tpNum = %s\n",pNum,pNum);

    printf("sizeof(buffer)  = %ld\n",sizeof(buffer));
    printf("sizeof(pNum)  = %ld\n",sizeof(pNum));

    printf("buffer = %c\n",*buffer);
    printf("*pNum = %c\n",*pNum);
    printf("buffer[0]=%c\n",buffer[0]);
    printf("*buffer+1 = %c\n",*buffer+1);
    printf("*(buffer+4)= %c\n",*(buffer+4));
    printf("*(pNum+4)=%c\n",*(pNum+4));
    printf("buffer[4]=%c\n",buffer[4]);

    /*
        数组其实就是指针，指针就是数组.
        *（buffer + x)=buffer[x] 
    */
#endif

#if 0
    /*二维数组和二级指针*/
    int nums[3][4];
    int count = 0;
    for (int row = 0; row < 3; row++)
    {
        for (int col = 0; col < 4; col++)
        {
            /*局部变量：定义在{}内，离开{}生命周期就结束了*/
            // static int a = 1;
            nums[row][col] = ++count;
            printf("nums[%d][%d]=%d\t", row, col, nums[row][col]);
        }
        printf("\n");
    }

    printf("nums=%p\n",nums);
    printf("nums[0]=%p\n",nums[0]);
    printf("&nums[0][0]=%p\n",&nums[0][0]);
    /*二维数组：[数组名(地址)，数组第一行，数组首元素地址]是一样的.*/
    printf("nums[0] = %p\n",nums[0]);
    printf("nums[1] = %p\n",nums[1]);
    printf("nums[2] = %p\n",nums[2]);

    
    printf("*(nums + 0) = %p\n",*(nums + 0));
    printf("*(nums + 1) = %p\n",*(nums + 1));
    printf("*(nums + 2) = %p\n",*(nums + 2));

    printf("*(*(nums + 1) + 2) =%d\n",*(*(nums + 1) + 2));
    printf("nums[1][2] = %d\n",nums[1][2]);

    /*结论:nums[x][y] = *(*(nums + x) + y)*/

#endif

#if 0
    /*字符数组*/
    char buffer[32] = "hello world 271 class.";


    /*局部变量都是在栈空间，除了静态局部变量*/
    /*生命周期：计算机自己管理，于程序员无关，生命周期在{}内*/
    int length = sizeof(buffer);
    printf("length = %d\n",length); 

    int len = sizeof(buffer)/sizeof(buffer[0]);
#if 0
    int count = 0;
    for(int idx = 0; idx < len; idx++)
    {
        if(buffer[idx] >= 'a' && buffer[idx] <= 'z')
        {
            count++;
        }
    }
    printf("小写字母有%d个\n",count);
#endif

    /*calculate*/
    int len1=strlen(buffer);
    printf("len1 = %d\n",len1);

    /*strlen函数*/
    // int cnt = 0;
    //     for(int idx = 0; idx < len; idx++)
    // {
    //     if(buffer[idx] =='\0')
    //     {
    //         break;
    //     }
    //     else
    //     {
    //         cnt++;
    //     }
        
    // }
    // printf("cnt = %d\n",cnt);

 

#endif

#if 0
/*字符串:全局常量区，不可以被修改*/
/*字符串本身就是地址*/
    char *str = "hello";

    int size = sizeof(str);
    printf("size = %d\n",size);

    int length = strlen(str);
    printf("length = %d\n",length);

    // for(int idx = 0;idx < (length/2);idx++)
    // {
    //     char temp = *(str+idx);
    //     *(str+idx) = *(str+length-1-idx);
    //     *(str+length-1-idx) = temp;
    // }

#endif

#if 1

    /*字符串函数*/
    /*strlen计算字符串处理
        strcmp比较两个字符串
        strcatlj*/
    char *str1 = "zhangsan";
    char *str2 = "lisidfsaxss";

    int ret = strcmp(str1,str2);
    printf("ret:%d\n",ret);

    char dst[32] = "hello";
    char *src = "world";

    strcat(dst,src);

    printf("%s",dst);
#endif
    return 0;
}